﻿#region namespaces

using System;
using System.Drawing;

using SLIITPro.Core;
using SLIITPro.Data.Extension;
using SLIITPro.Data.Entity;

#endregion

namespace SLIITPro
{
    public partial class Register : System.Web.UI.Page
    {
        private Users _users;
        private UserExtension _userext;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack) return;
            Utility.BindCountries(cmbCountry);
            if (Page.User.IsInRole("administrator"))
            {
                cmbUserType.Items.Add("content");
                cmbUserType.Items.Add("administrator");
            }
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            Page.Validate("user");
            if (Page.IsValid)
            {
                _userext = new UserExtension();
                if (_userext.IsEmailExist(txtEmail.Text))
                {
                    lblMessage.Text = string.Format("Email ({0}) already registered on system", txtEmail.Text);
                    lblMessage.ForeColor = Color.Red;
                    return;
                }
                string activationCode = Guid.NewGuid().ToString();
                _users = new Users
                             {
                                 FirstName = txtFirstName.Text,
                                 LastName = txtLastName.Text,
                                 Street = txtStreet.Text,
                                 City = txtCity.Text,
                                 Province = txtProvince.Text,
                                 Country = cmbCountry.SelectedValue,
                                 Phone = txtPhone.Text,
                                 Fax = txtFax.Text,
                                 Email = txtEmail.Text,
                                 Password = txtPassword.Text,
                                 UserType = cmbUserType.SelectedValue,
                                 CreatedAt = DateTime.Now,
                                 UpdatedAt = DateTime.Now,
                                 ActivationCode = activationCode,
                             };

                _userext.CreateUser(_users);
                SendConfirmationMail(activationCode);
                Utility.ClearContent(regTable);
                Response.Redirect(CoreSettings.RelativeWebRoot, true);
            }
        }

        private void SendConfirmationMail(string accCode)
        {
            string url = string.Format("{0}activation.aspx?ac={1}", CoreSettings.AbsoluteUrl, accCode);
            string datetime = "" + string.Format("{0} : ({1})", DateTime.Now.ToLongDateString(),
                                                         DateTime.Now.ToShortTimeString());
            string content = string.Format(Properties.Resources.AccRegisterSucceededEmail,
                                            string.Format(" {0} {1}", txtFirstName.Text.Trim(), txtLastName.Text.Trim()),
                                            datetime, txtEmail, txtPassword);
            using (var template = LoadControl(CoreSettings.TemplateRoot + "MailTemplate.ascx"))
            {
                ((IMailTemplate)template).UserName = string.Format("{0} {1}", txtFirstName.Text.Trim(), txtLastName.Text.Trim());
                ((IMailTemplate)template).UrlNavigation = url;
                ((IMailTemplate)template).UrlText = url;
                ((IMailTemplate)template).Body = content;
                ((IMailTemplate)template).UrlMessage = "before you log into system you have to activate your account using following URL";

                var body = Utility.BuildTemplate(template);

                Utility.SendEmail(txtEmail.Text.Trim(), "LBIS", "Account Activation", body);
                lblMessage.Text = "Password reset confirmation sent succeeded";
            }
        }
    }
}